package com.ruoyi.project.system.task.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.task.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.task.mapper.TransferMapper;
import com.ruoyi.project.system.task.domain.Transfer;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TransferServiceImpl implements TransferService{

    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private TaskMapper taskMapper;

    /**
     * 根据条件查询任务转移
     * @param transfer
     * @return
     */
    @Override
    public List<Transfer> selectTransferList(Transfer transfer) {
        return transferMapper.selectTranfList(transfer);
    }

    /**
     * 新增任务转移
     * @param transfer
     * @return
     */
    @Override
    @Transactional
    public int insertTranf(Transfer transfer) {
        transfer.setCreateBy(ShiroUtils.getLoginName());
        int i = transferMapper.insertTranf(transfer);
        ShiroUtils.clearCachedAuthorizationInfo();
        return i;
    }

    /**
     * 校验任务id是否存在并且已审核通过
     * @param transfer
     * @return
     */
    @Override
    public String checkTranfTaskIdUnique(Transfer transfer) {
        Transfer info = transferMapper.checkTranfTaskIdUnique(transfer.getTaskId());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.TRANF_TASKID_UNIQUE_AND_EXIST;
        }
        return UserConstants.TRANF_TASKID_NOT_UNIQUE_AND_EXIST;
    }

    /**
     * 根据转移id查询转移任务
     * @param transferId
     * @return
     */
    @Override
    public Transfer selectTranfById(Integer transferId) {
        return transferMapper.selectByPrimaryKey(transferId);
    }

    /**
     * 根据信息修改任务转移
     * @param transfer
     * @return
     */
    @Override
    @Transactional
    public int updateTranfTask(Transfer transfer) {
        transfer.setCreateBy(ShiroUtils.getLoginName());
        System.out.println(transfer.toString());
        System.out.println("============================"+transfer.getPass());
        System.out.println("============================"+transfer.getSuccessor());
        if(transfer.getPass() == 1 && transfer.getSuccessor() != null){
            System.out.println("+++++++++++++++++++Yes++++++++++++++++++");
            //当审核通过并且存在接替人时修改接替者为任务执行人
            taskMapper.updateByExecutor(transfer.getSuccessor(),transfer.getTaskId());
        }
        ShiroUtils.clearCachedAuthorizationInfo();
        return transferMapper.updateByPrimaryKey(transfer);
    }

    /**
     * 根据转移id批量删除
     * @param transfers
     * @return
     */
    @Override
    @Transactional
    public int deleteTranfByIds(String transfers) {
        return transferMapper.deleteByTransferIds(Convert.toStrArray(transfers));
    }


}
